
/*-----------------------------碰撞模块---------------------------*/
export default function collisions(deltaTime){
    gravity(deltaTime)
    const result = worldOctree.capsuleIntersect( playerCollider );
    playerOnFloor = false;
    if ( result ) {
        playerOnFloor = result.normal.y > 0;
        if ( ! playerOnFloor ) {
            playerVelocity.addScaledVector( result.normal, - result.normal.dot( playerVelocity ) );
        }
        let deltaPosition=result.normal.multiplyScalar( result.depth )
        player.position.add(deltaPosition)
    } 
    if(showCollider)showColliderMode()
    resetPlayer()
}
// 同步玩家与碰撞体 添加重力效果
function gravity(deltaTime){
    let damping = Math.exp( - 4 * deltaTime ) - 1;
    if ( ! playerOnFloor ) {
        playerVelocity.y -= GRAVITY * deltaTime;
        damping *= 0.1;
    }
    playerVelocity.addScaledVector( playerVelocity, damping );
    const deltaPosition = playerVelocity.clone().multiplyScalar( deltaTime );
    player.position.add(deltaPosition)
    // 碰撞体与玩家位置同步
    playerCollider.setPosition(player.position.clone())
}

// 重置玩家位置
function resetPlayer() {
    if ( player.position.y <= - 25 ) {
        playerCollider.start.set( 0, 0.35, 0 );
        playerCollider.end.set( 0, 1.4, 0 );
        playerCollider.radius = 0.35;
        player.position.copy( playerCollider.end );
        player.rotation.set( 0, 0, 0 );
    }
}

// 显示碰撞范围
function showColliderMode(){
    sphere1.position.copy(playerCollider.start)
    sphere2.position.copy(playerCollider.end)
    let target=playerCollider.start
    body.position.copy(target.add(new Vector3(0,0.525,0)))
}
  